package com.seed.uaa.util;

import cn.hutool.core.util.StrUtil;
import com.seed.core.exception.UserAuthenticateException;
import com.seed.core.exception.UserNotExistException;
import com.seed.uaa.web.organization.Organization;
import com.seed.uaa.web.user.User;
import com.seed.core.enumeration.YesNo;
import com.seed.uaa.tenant.web.Tenant;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

public class LoginCheckUtil {

    public static void checkLogin(Organization organization) {
        if(organization == null) {
            throw new UserNotExistException("组织不存在");
        }

        String code = organization.getCode();
        String reason = organization.getReason();
        if(organization.getUsable() != YesNo.yes) {
            String format = "组织账号【%s】不可用，原因：【%s】";
            throw new UserAuthenticateException(String.format(format, code, StrUtil.isBlank(reason) ? "无" : reason));
        }

        if(organization.getLoginable() != YesNo.yes) {
            String format = "组织账号【%s】不允许登录，原因：【%s】";
            throw new UserAuthenticateException(String.format(format, code, StrUtil.isBlank(reason) ? "无" : reason));
        }

        if(organization.getEnable() != YesNo.yes) {
            String format = "组织账号【%s】被禁用，原因：【%s】";
            throw new UserAuthenticateException(String.format(format, code, StrUtil.isBlank(reason) ? "无" : reason));
        }

        LocalDate expireDate = organization.getExpireDate();
        if(expireDate != null && expireDate.isBefore(LocalDate.now())) {
            throw new UserAuthenticateException(String.format("组织账号【%s】已过期", code));
        }
    }

    public static void checkLogin(User user) {
        if(user == null) {
            throw new UserNotExistException();
        }

        if(user.getActive() != YesNo.yes) {
            throw new UserAuthenticateException("账号未激活");
        }

        String username = user.getUsername();
        String reason = user.getReason();
        if(user.getUsable() != YesNo.yes) {
            String format = "用户账号【%s】不可用，原因：【%s】";
            throw new UserAuthenticateException(String.format(format, username, StrUtil.isBlank(reason) ? "无" : reason));
        }

        if(user.getLoginable() != YesNo.yes) {
            String format = "用户账号【%s】不允许登录，原因：【%s】";
            throw new UserAuthenticateException(String.format(format, username, StrUtil.isBlank(reason) ? "无" : reason));
        }

        if(user.getEnable() != YesNo.yes) {
            String format = "用户账号【%s】被禁用，原因：【%s】";
            throw new UserAuthenticateException(String.format(format, username, StrUtil.isBlank(reason) ? "无" : reason));
        }

        LocalDateTime expireDate = user.getExpireDate();
        if(expireDate != null && expireDate.isBefore(LocalDateTime.now())) {
            throw new UserAuthenticateException(String.format("用户账号【%s】已过期", username));
        }
    }

    public static void checkLogin(Tenant tenant) {
        if(tenant == null) {
            throw new UserNotExistException();
        }

        String tenantname = tenant.getTenantname();
        if(tenant.getActive() != YesNo.yes) {
            throw new UserAuthenticateException(String.format("租户账号【%s】未激活", tenantname));
        }

        String reason = tenant.getReason();
        if(tenant.getUsable() != YesNo.yes) {
            String format = "租户账号【%s】不可用，原因：【%s】";
            throw new UserAuthenticateException(String.format(format, tenantname, StrUtil.isBlank(reason) ? "无" : reason));
        }

        if(tenant.getLoginable() != YesNo.yes) {
            String format = "租户账号【%s】不允许登录，原因：【%s】";
            throw new UserAuthenticateException(String.format(format, tenantname, StrUtil.isBlank(reason) ? "无" : reason));
        }

        if(tenant.getEnable() != YesNo.yes) {
            String format = "租户账号【%s】被禁用，原因：【%s】";
            throw new UserAuthenticateException(String.format(format, tenantname, StrUtil.isBlank(reason) ? "无" : reason));
        }

        LocalDateTime expireDate = tenant.getExpireDate();
        if(expireDate != null && expireDate.isBefore(LocalDateTime.now())) {
            throw new UserAuthenticateException(String.format("租户账号【%s】已过期", tenantname));
        }
    }

    /**
     * 检查给定的组织的祖先是否可以登录
     */
    public static void checkAncestorLogin(List<Organization> ancestorList) {
        for(Organization ancestor: ancestorList) {
            if(ancestor.getUsable() != YesNo.yes || ancestor.getLoginable() != YesNo.yes) {
                throw new UserAuthenticateException(ancestor.getReason());
            }

            String code = ancestor.getCode();
            String reason = ancestor.getReason();
            if(ancestor.getUsable() != YesNo.yes) {
                String format = "祖先账号【%s】不可用，原因：【%s】";
                throw new UserAuthenticateException(String.format(format, code, StrUtil.isBlank(reason) ? "无" : reason));
            }

            if(ancestor.getLoginable() != YesNo.yes) {
                String format = "祖先账号【%s】不允许登录，原因：【%s】";
                throw new UserAuthenticateException(String.format(format, code, StrUtil.isBlank(reason) ? "无" : reason));
            }

            LocalDate expireDate = ancestor.getExpireDate();
            if(expireDate != null && expireDate.isBefore(LocalDate.now())) {
                throw new UserAuthenticateException(String.format("祖先账号【%s】已过期", code));
            }
        }
    }


    /**
     * 检查给定的组织的祖先是否可以登录
     */
    public static void checkAncestorLogin(Organization ancestor) {
        if(ancestor.getUsable() != YesNo.yes || ancestor.getLoginable() != YesNo.yes) {
            throw new UserAuthenticateException(ancestor.getReason());
        }

        String code = ancestor.getCode();
        String reason = ancestor.getReason();
        if(ancestor.getUsable() != YesNo.yes) {
            String format = "祖先账号【%s】不可用，原因：【%s】";
            throw new UserAuthenticateException(String.format(format, code, StrUtil.isBlank(reason) ? "无" : reason));
        }

        if(ancestor.getLoginable() != YesNo.yes) {
            String format = "祖先账号【%s】不允许登录，原因：【%s】";
            throw new UserAuthenticateException(String.format(format, code, StrUtil.isBlank(reason) ? "无" : reason));
        }

        LocalDate expireDate = ancestor.getExpireDate();
        if(expireDate != null && expireDate.isBefore(LocalDate.now())) {
            throw new UserAuthenticateException(String.format("祖先账号【%s】已过期", code));
        }
    }
}
